package com.qianwen.wallpaper;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;

/**
 * 配置缓存
 *
 * @author qianwen
 * @since 2023/11/27
 */
public class ConfigCache {
    /**
     * 基础路径
     */
    public static final String BASE_PATH =
            Environment.getExternalStorageDirectory().getPath() + File.separator;

    public static final String FILE_PATH = "Pictures/wallpaper";

    private static final String TAG = "ConfigCache";

    private static final String CACHE_FILE_NAME = "qianwenConfig";

    private static final String KEY_PATH = "path";

    private static final String KEY_DURATION = "duration";

    private static final String KEY_DESK_SWITCH = "isDeskSwitch";

    private static final String KEY_lock_SWITCH = "isLockSwitch";

    private static final int DEFAULT_DURATION = 5;

    private static String PATH;

    private static Boolean IS_DESK_SWITCH;

    private static Boolean IS_LOCK_SWITCH;

    private static Integer DURATION;

    /**
     * 获取定时时间
     *
     * @param context 上下文 上下文 上下文
     * @return 自动切换时间间隔
     */
    public static int getDuration(Context context) {
        if (DURATION != null) {
            return DURATION;
        }
        if (context == null) {
            Log.e(TAG, "getDuration: context is null");
            return DEFAULT_DURATION;
        }
        SharedPreferences preferences = context.getSharedPreferences(CACHE_FILE_NAME,
                Context.MODE_PRIVATE);
        return preferences.getInt(KEY_DURATION, DEFAULT_DURATION);
    }

    /**
     * 设置自动切换时间间隔
     *
     * @param context  上下文 上下文  上下文
     * @param duration 自动切换时间间隔
     */
    public static void setDuration(Context context, int duration) {
        if (context == null || duration <= 0) {
            Log.e(TAG, "setDuration: context or duration invalid");
            return;
        }
        SharedPreferences preferences = context.getSharedPreferences(CACHE_FILE_NAME,
                Context.MODE_PRIVATE);
        DURATION = duration;
        preferences.edit().putInt(KEY_DURATION, duration).apply();
    }

    /**
     * 获取桌面壁纸开关
     *
     * @param context 上下文 上下文 上下文
     * @return 开关状态
     */
    public static boolean getDeskSwitch(Context context) {
        if (IS_DESK_SWITCH != null) {
            return IS_DESK_SWITCH;
        }
        if (context == null) {
            Log.e(TAG, "getDeskSwitch: context is null");
            return true;
        }
        SharedPreferences preferences = context.getSharedPreferences(CACHE_FILE_NAME,
                Context.MODE_PRIVATE);
        return preferences.getBoolean(KEY_DESK_SWITCH, true);
    }

    /**
     * 保存桌面壁纸开发
     *
     * @param context 上下文
     * @param value   开关
     */
    public static void setDeskSwitch(Context context, boolean value) {
        if (context == null) {
            Log.e(TAG, "setDeskSwitch: context is null");
            return;
        }
        SharedPreferences preferences = context.getSharedPreferences(CACHE_FILE_NAME,
                Context.MODE_PRIVATE);
        IS_DESK_SWITCH = value;
        preferences.edit().putBoolean(KEY_DESK_SWITCH, value).apply();
    }

    /**
     * 获取锁屏壁纸开关
     *
     * @param context 上下文
     * @return 开关
     */
    public static boolean getLockSwitch(Context context) {
        if (IS_LOCK_SWITCH != null) {
            return IS_LOCK_SWITCH;
        }
        if (context == null) {
            Log.e(TAG, "getLockSwitch: context is null");
            return true;
        }
        SharedPreferences preferences = context.getSharedPreferences(CACHE_FILE_NAME,
                Context.MODE_PRIVATE);
        return preferences.getBoolean(KEY_lock_SWITCH, true);
    }

    /**
     * 保存锁屏壁纸开关
     *
     * @param context 上下文
     * @param value   开关
     */
    public static void setLockSwitch(Context context, boolean value) {
        if (context == null) {
            Log.e(TAG, "setDeskSwitch: context is null");
            return;
        }
        SharedPreferences preferences = context.getSharedPreferences(CACHE_FILE_NAME,
                Context.MODE_PRIVATE);
        IS_LOCK_SWITCH = value;
        preferences.edit().putBoolean(KEY_lock_SWITCH, value).apply();
    }

    /**
     * 获取壁纸路径
     *
     * @return 壁纸路径
     */
    public static String getPath(Context context) {
        if (!TextUtils.isEmpty(PATH)) {
            return PATH;
        }
        if (context == null) {
            Log.e(TAG, "getPath: context is empty");
            return FILE_PATH;
        }
        SharedPreferences preferences = context.getSharedPreferences(CACHE_FILE_NAME,
                Context.MODE_PRIVATE);
        return preferences.getString(KEY_PATH, FILE_PATH);
    }

    /**
     * 保存路径
     *
     * @param context 上下文 上下文 上下文
     * @param path    路径
     */
    public static void setPath(Context context, String path) {
        if (context == null || TextUtils.isEmpty(path)) {
            Log.e(TAG, "setPath: context or path is empty");
            return;
        }
        if (!new File(BASE_PATH + path).exists()) {
            Log.e(TAG, "setPath: invalid path");
            return;
        }
        PATH = path;
        SharedPreferences preferences = context.getSharedPreferences(CACHE_FILE_NAME,
                Context.MODE_PRIVATE);
        preferences.edit().putString(KEY_PATH, path).apply();
    }

    /**
     * 私有化构造器
     */
    private ConfigCache() {
    }
}
